# -*- coding: UTF-8 -*-

import scrapy
import pymysql.cursors
import urlparse
import sys
reload(sys)
sys.setdefaultencoding('utf8')
class FoodsSpider(scrapy.Spider):
    name = "movie"
    allowed_domains = ["food.hiyd.com"]
    start_urls = [
        "https://food.hiyd.com/list-1-html"
    ]
    #conn = pymysql.connect(host='127.0.0.1',user='root',password='198482115',db='insights',charset='utf8',cursorclass=pymysql.cursors.DictCursor)
    def parse(self, response):

        #li = response.xpath('//*[@id="data_list"]/li/div')
        #net = response.xpath("//div[contains(@class,'page')]/a[last()-1]")
        #next = urljoin(response.url,net.xpath('@href').extract_first())
        #next_text = net.xpath('text()').extract_first()
        #print(next_text,next)
        #print(next)
        list = response.xpath("//*[@class='box-bd']/ul/li")
        for li in list:
            alink = li.xpath('a/@href').extract_first()
            print(alink)
        last_btn = response.xpath('//*[@class="mod-page"]/a[last()]')
        next = urlparse.urljoin(response.url,last_btn.xpath('@href').extract_first())
        next_text = last_btn.xpath('text()').extract_first()
        print(next_text)
        if(next_text=='下一页'):
            yield scrapy.Request(next,callback=self.parse)

